#include "QColorProperty.h"
#include "QColorPropertyEditor.h"

#include "private/QProperty_p.h"

using namespace QPE;

QColorProperty::QColorProperty(const QString &name) :
	QProperty(name, QVariant::Color)
{
}


//! Get the data how it should be displayed
QVariant QColorProperty::data (int column, int role) const
{
	if(column == DPC_Data && (Qt::DisplayRole == role))
		return QColorPropertyEditor::getColorString(d_ptr->VariantValue.value<QColor>());
	else if(Qt::EditRole == role)
		return QVariant();
	else if(column == DPC_Data && (Qt::DecorationRole == role))
		return QColorPropertyEditor::getColorPixmap(d_ptr->VariantValue.value<QColor>());
	else
		return QProperty::data(column, role);
}

//! Returns an editor widget, or NULL if it doesn't supply one
QWidget* QColorProperty::createEditor(QWidget* parent, const QStyleOptionViewItem& options, const QAbstractItemDelegate* delegate)
{
	Q_UNUSED(options);
	Q_UNUSED(delegate);

	QColorPropertyEditor* tmpWidget = new QColorPropertyEditor(parent);
	tmpWidget->setColor(d_ptr->VariantValue.value<QColor>());
	return tmpWidget;
}

//! Sets the property's data to the editor (returns false, if the standard delegate should do that)
bool QColorProperty::setEditorData(QWidget* editor)
{
	QColorPropertyEditor* tmpWidget = qobject_cast<QColorPropertyEditor*>(editor);
	if(tmpWidget)
		tmpWidget->setColor(d_ptr->VariantValue.value<QColor>());
	else
		return false;

	return true;
}

//! Gets the data from the widget
QVariant QColorProperty::getEditorData(QWidget* editor) const
{
	QColorPropertyEditor* tmpWidget = qobject_cast<QColorPropertyEditor*>(editor);
	if(tmpWidget)
		return tmpWidget->getColor();

	return QVariant();
}

QString QColorProperty::type() const
{
	return "color";
}

QProperty *QColorProperty::clone(bool include_children, QProperty *container) const
{
	return QProperty::clone(include_children, container ? container : new QColorProperty(getName()));
}
